<template>
  <div class="goods-image">
    <div
      v-show="show"
      class="large"
      :style="{
        backgroundImage: `url(${images[currentIndex]})`,
        backgroundPositionY: bgPosition.top + 'px',
        backgroundPositionX: bgPosition.left + 'px',
      }"
    ></div>
    <div class="middle" ref="target">
      <div
        v-show="show"
        class="layer"
        :style="{
          top: layerPosition.top + 'px',
          left: layerPosition.left + 'px',
        }"
      ></div>
      <img :src="images[currentIndex]" alt="" />
    </div>
    <ul class="small">
      <li
        :class="{ active: currentIndex === index }"
        v-for="(item, index) in images"
        :key="index"
        @mouseenter="currentIndex = index"
      >
        <img :src="item" alt="" />
      </li>
    </ul>
  </div>
</template>
<script>
import { ref, watch } from "vue";
import { useMouseInElement } from "@vueuse/core";
export default {
  name: "GoodsImages",
  props: {
    images: {
      type: Array,
      default: () => [],
    },
  },
  setup() {
    // 定义索引
    const currentIndex = ref(0);
    // 保存镜片到元素的距离;
    const layerPosition = ref({ left: 0, top: 0 });
    // 保存大图背景位置/;
    const bgPosition = ref({ left: 0, top: 0 });
    // 获取元素
    const target = ref(null);
    // 控制镜片和预览显示隐藏
    const show = ref(false);
    // 获取鼠标与元素之间的距离(useMouseInElement结构的元素就是响应式,使用是需要加value)
    /**
     * isOutside  鼠标是否进入元素
     */
    const { isOutside, elementX, elementY } = useMouseInElement(target);
    // 监听鼠标与元素的距离
    watch([isOutside, elementX, elementY], () => {
      // 更新镜片位置
      layerPosition.value = {
        left: elementX.value - 100,
        top: elementY.value - 100,
      };
      // 判断范围 左右
      if (layerPosition.value.left < 0) {
        layerPosition.value.left = 0;
      } else if (layerPosition.value.left > 200) {
        layerPosition.value.left = 200;
      }
      // 判断范围上下
      if (layerPosition.value.top < 0) {
        layerPosition.value.top = 0;
      } else if (layerPosition.value.top > 200) {
        layerPosition.value.top = 200;
      }
      // 鼠标进入元素,镜片显示
      show.value = !isOutside.value;
      bgPosition.value = {
        left: -layerPosition.value.left * 2,
        top: -layerPosition.value.top * 2,
      };
    });
    return {
      currentIndex,
      layerPosition,
      target,
      bgPosition,
      show,
    };
  },
};
</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  z-index: 500;
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }
  .middle {
    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    position: relative;
    cursor: move;
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
